package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.utils.PrintUtils;

public class SearchInRotatedSortedArray2 {

    /**
     * 二分查找法
     */
    public boolean search(int[] nums, int target) {
        int m = nums.length;
        int k = m - 1;
        while (k > 0 && nums[k] >= nums[k - 1]){
            k--;
        }
        int left = 0, right = m - 1;
        while (left <= right){
            int mid = (left + right) / 2;
            int newMid = (mid + k) % m;
            if (nums[newMid] > target){
                right = mid - 1;
            } else if(nums[newMid] < target){
                left = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] nums = { 2,2,2,3,2,2,2};
        int target = 3;
        boolean search = new SearchInRotatedSortedArray2().search(nums, target);
        System.out.println(search);
    }
}
